﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RoleList.aspx.cs" Inherits="RoleList" %>

<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>欢迎使用工会管理系统</title>
    <link href="../style/main.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../css/css.css" />
    <link rel="stylesheet" type="text/css" href="/css/jquery-ui-1.7.2.custom.css" />
    <link rel="stylesheet" type="text/css" href="../JqueryUI/themes/ui-lightness/jquery-ui-1.7.2.custom.css">

    <script type="text/javascript" src="../javascript/jquery-1.3.2.js"></script>

    <script type="text/javascript" src="../javascript/jquery-ui-1.7.2.custom.js"></script>

    <script type="text/javascript" src="../javascript/messageBox.js"></script>

    <script type="text/javascript" src="../javascript/formValidator.js"></script>

    <script type="text/javascript" src="../javascript/formValidatorRegex.js"></script>

    <script type="text/javascript" src="../javascript/Public.js"></script>

    <script type="text/javascript" src="../javascript/RoleList.aspx.js"></script>

    <script type="text/javascript" language="javascript">

        function isExist() {


            var queryString = "";
            var FormFlg = "Role"; //处理画面区分，对应不同的后台业务处理                
            var strName = $("#txtRoleName").val();

            queryString = "FormFlg=" + FormFlg
                               + "&strName=" + strName
                               + "&id=" + $("#txtRoleId").val();
            $.ajax
                        ({
                            async: false,
                            type: "post", //ajax请求方式
                            url: "../AjaxExist.aspx", //请求服务器地址
                            datatype: "json", //希望数据库返回的数据类型
                            data: queryString, //发送给服务器的数据
                            processData: false, //阻止数据被自动转换为url编码
                            success: function(data) {//请求成功则调用该函数
                                if (data == "1") {
                                    $("#txtState").val("");
                                    alert("角色已存在！");
                                    return;
                                } else {
                                    $("#txtState").val("OK");
                                    return;
                                }
                            }
                        });

        }

        $(document).ready(function() {
            var formValid = false; //表单验证是否成功flg

            var formValid = false; //表单验证是否成功flg
            jQuery.formValidator.initConfig({ validatorgroup: "1", tidymode: false, forcevalid: false, alertmessage: false, errorfocus: true, onerror: function(msg) { alert(msg) }, onsuccess: function() { formValid = true; return true; } });
            //角色代码
            $("#txtRoleName").formValidator({ validatorgroup: "1", empty: false })
	                    .functionValidator({ fun: isemptycheck, onerror: ShowMessage("角色名称", "1001") })//不可省略check
	                     .inputValidator({ min: 1, max: 15, onerror: ShowMessage("角色名称", "1005") })
            $("#txtState").formValidator({ validatorgroup: "1", empty: false })
	                     .inputValidator({ min: 1, max: 2, onerror: ShowMessage("角色", "1038") })




            $("#RoleEdit").dialog({
                bgiframe: true,
                autoOpen: false,
                height: 200,
                modal: true,
                draggable: true,
                buttons: {
                    '提交': function() {

                        jQuery.formValidator.pageIsValid('1');
                        if (!formValid) {
                            return false;
                        }
                        if (Update(btnName)) {
                            $(this).dialog('close');
                            var submitForm = document.getElementById("mainForm");
                            submitForm.submit();
                        }
                    },
                    '取消': function() {
                        $(this).dialog('close');
                    }
                },
                close: function() {
                    //allFields.val('');
                }

            });

            //-------Main Start--------------------------------------------------------------

            //处理区分
            var btnName = "";
            function createQueryString() {
                var queryString = "";
                var RoleId = $("#txtRoleId").val();
                var RoleName = $("#txtRoleName").val();
                var Remark = $("#txtRemark").val();
                var UpdateFlg = btnName; //操作区分，更新或者新规
                var FormFlg = "Role"; //处理画面区分，对应不同的后台业务处理
                queryString = "FormFlg=" + FormFlg
                + "&UpdateFlg=" + UpdateFlg
                + "&RoleId=" + RoleId
                + "&RoleName=" + RoleName
                + "&Remark=" + Remark;
                return queryString;
            }
            function Update(operflg) {
                var addValid = false; //插入操作是否成功flg
                $.ajax
                        ({
                            async: false,
                            type: "post", //ajax请求方式
                            url: "../ajaxUpdate.aspx", //请求服务器地址
                            datatype: "json", //希望数据库返回的数据类型
                            data: createQueryString(), //发送给服务器的数据
                            processData: false, //阻止数据被自动转换为url编码
                            success: function(data) {//请求成功则调用该函数
                                if (data == "OK") {
                                    addValid = true;
                                    if (operflg == "Add") {
                                        alert("登录成功！")
                                    }
                                    else if (operflg == "Update") {
                                        alert("更新成功！")
                                    }
                                    var submitForm = document.getElementById("mainForm");
                                    submitForm.submit();
                                }
                                else {
                                    addValid = false;
                                    if (operflg == "Add") {
                                        alert("登录失败！")
                                    }
                                    else if (operflg == "Update") {
                                        alert("更新失败！")
                                    }
                                }
                            }
                        });
                return addValid;
            };

            $("#btnAdd").click(function() {
                btnName = "Add";
                $("#RoleEdit").dialog('open');
            });


            $(".btnUpdate").click(function() {
                btnName = "Update";
                $('#txtRoleId').val($.trim($(this).parent().find('td:eq(0)').text()));
                $('#txtRoleName').val($.trim($(this).parent().find('td:eq(1)').text()));
                $('#txtRemark').val($.trim($(this).parent().find('td:eq(2)').text()));
                $("#RoleEdit").dialog('open');
            });

            $(".btnDelete").click(function() {
                var queryString = "";
                var FormFlg = "Role"; //处理画面区分，对应不同的后台业务处理
                var UpdateFlg = "Delete";   //操作区分，更新或者新规
                var RoleId = jQuery.trim(jQuery(this).parent().find('td:eq(0)').text());
                queryString = "FormFlg=" + FormFlg
                               + "&UpdateFlg=" + UpdateFlg
                               + "&RoleId=" + RoleId;
                $.ajax
                        ({
                            async: false,
                            type: "post", //ajax请求方式
                            url: "../ajaxUpdate.aspx", //请求服务器地址
                            datatype: "json", //希望数据库返回的数据类型
                            data: queryString, //发送给服务器的数据
                            processData: false, //阻止数据被自动转换为url编码
                            success: function(data) {//请求成功则调用该函数
                                if (data == "OK") {
                                    alert("删除成功！");
                                    var submitForm = document.getElementById("mainForm");
                                    submitForm.submit();
                                } else {

                                    alert("删除失败！");
                                }
                            }
                        });

            });

            //-------Main End--------------------------------------------------------------
            var CreateRoleID = 0;

            //---SetRole  Start-----------------------------------------------------------
            $("#SetRole").dialog({
                bgiframe: true,
                autoOpen: false,
                width: 370,
                height:490,
                modal: true,
                draggable: true,
                buttons: {
                    '确定': function() {
                        if (UpdateRole("Add")) {
                            $(this).dialog('close');
                        }
                    },
                    '取消': function() {
                        $(this).dialog('close');
                    }
                },
                close: function() {
                    //allFields.val('');
                }
            });

            function UpdateRole(operflg) {
                var addValid = false; //插入操作是否成功flg
                $.ajax
                        ({
                            async: false,
                            type: "post", //ajax请求方式
                            url: "../ajaxUpdate.aspx", //请求服务器地址
                            datatype: "json", //希望数据库返回的数据类型
                            data: CreateRoleGroupString(), //发送给服务器的数据
                            processData: false, //阻止数据被自动转换为url编码
                            success: function(data) {//请求成功则调用该函数
                                if (data == "OK") {
                                    addValid = true;
                                    if (operflg == "Add") {
                                        alert("登录成功！")
                                    }
                                    else if (operflg == "Update") {
                                        alert("更新成功！")
                                    }
                                    var submitForm = document.getElementById("mainForm");
                                    submitForm.submit();
                                }
                                else {
                                    addValid = false;
                                    if (operflg == "Add") {
                                        alert("登录失败！")
                                    }
                                    else if (operflg == "Update") {
                                        alert("更新失败！")
                                    }
                                }
                            }
                        });
                return addValid;
            };

            function getCheckData() {
                var data = "";

                $(":checkbox").each(function() {
                    if ($(this).attr("checked") == true) {
                        data += ($(this).val() + "|");
                    }
                });
                return data.toString();
            }

            //选择权限的json数据
            function CreateRoleGroupString() {
                var queryString = "";
                var FormFlg = "RoleSet"; //处理画面区分，对应不同的后台业务处理
                var UpdateFlg = "UpdateRole"; //操作区分，更新或者新规
                queryString = "FormFlg=" + FormFlg
                            + "&UpdateFlg=" + UpdateFlg
                            + "&Actions=" + getCheckData()
                            + "&RoleID=" + CreateRoleID;
               return queryString;
            }

            function SelectRole(RoleID) {
                $.ajax({
                    async: false,
                    type: "post", //ajax请求方式
                    url: "../SystemManage/RoleList.aspx", //请求服务器地址
                    datatype: "json", //希望数据库返回的数据类型
                    data: "RoleID=" + RoleID, //发送给服务器的数据
                    processData: false, //阻止数据被自动转换为url编码
                    success: function(data) {//请求成功则调用该函数
                        $("#SetRoleHtml").empty();
                        $("#SetRoleHtml").html(data);
                        $("#SetRole").dialog('open');
                    }
                });
                // return addValid;
            };

            $(".btnRoleSet").click(function() {
                CreateRoleID = $.trim($(this).closest('tr').find('>td:eq(0)').text());
                SelectRole(CreateRoleID);
            });
            //---SetRole  End-----------------------------------------------------------


            //---SetUserGroup  Start-----------------------------------------------------------

            $("#SetUserGroup").dialog({
                bgiframe: true,
                autoOpen: false,
                width: 300,
                height: 280,
                modal: true,
                draggable: true,
                buttons: {
                    '确定': function() {
                        if (UpdateUserGroup("Add")) {
                            $(this).dialog('close');
                        }
                    },
                    '取消': function() {
                        $(this).dialog('close');
                    }
                },
                close: function() {
                    //allFields.val('');
                }
            });

            function UpdateUserGroup(operflg) {

                var addValid = false; //插入操作是否成功flg
                $.ajax
                        ({
                            async: false,
                            type: "post", //ajax请求方式
                            url: "../ajaxUpdate.aspx", //请求服务器地址
                            datatype: "json", //希望数据库返回的数据类型
                            data: CreateUserGroupString(), //发送给服务器的数据
                            processData: false, //阻止数据被自动转换为url编码
                            success: function(data) {//请求成功则调用该函数
                                if (data == "OK") {
                                    addValid = true;
                                    if (operflg == "Add") {
                                        alert("登录成功！")
                                    }
                                    else if (operflg == "Update") {
                                        alert("更新成功！")
                                    }
                                    var submitForm = document.getElementById("mainForm");
                                    submitForm.submit();
                                }
                                else {
                                    addValid = false;
                                    if (operflg == "Add") {
                                        alert("登录失败！")
                                    }
                                    else if (operflg == "Update") {
                                        alert("更新失败！")
                                    }
                                }
                            }
                        });
                return addValid;
            };



            //选择权限的json数据
            function CreateUserGroupString() {
                var queryString = "";
                var FormFlg = "RoleSet"; //处理画面区分，对应不同的后台业务处理
                var UpdateFlg = "SetRoleGroup"; //操作区分，更新或者新规
                queryString = "FormFlg=" + FormFlg
                            + "&UpdateFlg=" + UpdateFlg
                            + "&RoleID=" + CreateRoleID
                            + "&UserIDs=" + getUserData();

                return queryString;
            }

            function getUserData() {
                var data = "";

                $("#listUserTarget option").each(function() {
                    data += ($(this).val() + "|");

                });
                return data.toString();
            }

            function SelectUserGroup(RoleID) {
                $.ajax({
                    async: false,
                    type: "post", //ajax请求方式
                    url: "../SystemManage/RoleList.aspx", //请求服务器地址
                    datatype: "json", //希望数据库返回的数据类型
                    data: "RoleID=5", //发送给服务器的数据
                    processData: false, //阻止数据被自动转换为url编码
                    success: function(data) {//请求成功则调用该函数
                        $("#SetRoleHtml").html(data);
                        $("#SetRole").dialog('open');
                    }
                });
                return addValid;
            };

            $(".btnUserGroup").click(function() {
                CreateRoleID = $.trim($(this).closest('tr').find('>td:eq(0)').text());
                //SelectUserGroup(CreateRoleID);
                $("#SetUserGroup").dialog('open');
            });

            //---SetUserGroup  End-----------------------------------------------------------

        });
    </script>

    <style type="text/css">
        .style1
        {
            color: #FF0066;
        }
    </style>
</head>
<body>
    <form id="mainForm" runat="server">
    <input type="hidden" id="txtState" value="OK" />
    <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <table width="99%" cellpadding="0" cellspacing="0" class="table">
                    <tr>
                        <td valign="top">
                            <table width="100%" class="top" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td style="height: 22px;">
                                        &nbsp;<b>当前位置:</b>&nbsp;<a href="#">系统管理</a>&nbsp;>>&nbsp;角色管理
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td style="height: 5px">
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="middle" style="border: thin dotted #C0C0C0; background-color: #C5E5FC">
                            <img alt="" src="../images/submit.gif" id="btnAdd" />&nbsp;&nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td style="height: 5px">
                        </td>
                    </tr>
                    <tr>
                        <td valign="top">
                            <table style="width: 100%; background-color: Silver" cellpadding="1px" cellspacing="1px">
                                <tr>
                                    <th style="height: 20px; background-color: #B2DBF7; display: none;">
                                        角色ID
                                    </th>
                                    <th style="height: 20px; background-color: #B2DBF7">
                                        角色名称
                                    </th>
                                    <th style="height: 20px; background-color: #B2DBF7">
                                        备注
                                    </th>
                                    <th style="height: 20px; background-color: #B2DBF7" colspan="4">
                                        操作
                                    </th>
                                </tr>
                                <asp:Repeater ID="rptList" runat="server">
                                    <HeaderTemplate>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <tr>
                                            <td style="height: 22px; background-color: #FFFFFF; display: none;">
                                                <%# DataBinder.Eval(Container.DataItem, "角色ID")%>
                                            </td>
                                            <td style="height: 22px; width: 20%; background-color: #FFFFFF">
                                                <%# DataBinder.Eval(Container.DataItem, "角色名称")%>
                                            </td>
                                            <td style="height: 22px; width: 60%; background-color: #FFFFFF">
                                                <%# DataBinder.Eval(Container.DataItem, "备注")%>
                                            </td>
                                            <td style="height: 22px; background-color: #FFFFFF; text-align: center; cursor: pointer;"
                                                class="btnUserGroup">
                                                用户组设定
                                            </td>
                                            <td style="height: 22px; background-color: #FFFFFF; text-align: center; cursor: pointer"
                                                class="btnRoleSet">
                                                权限设定
                                            </td>
                                            <td style="height: 22px; background-color: #FFFFFF; text-align: center; cursor: pointer"
                                                class="btnUpdate">
                                                编辑
                                            </td>
                                            <td style="height: 22px; background-color: #FFFFFF; text-align: center; cursor: pointer"
                                                class="btnDelete">
                                                删除
                                            </td>
                                        </tr>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        </table>
                                    </FooterTemplate>
                                </asp:Repeater>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <div id="RoleEdit" title="角色编辑">
        <fieldset>
            <table>
                <tr style="display: none;">
                    <td align="right">
                        角色ID
                    </td>
                    <td>
                        <input type="text" name="txtRoleId" id="txtRoleId" />
                    </td>
                </tr>
                <tr>
                    <td align="right" style="width: 65px">
                        角色名称
                    </td>
                    <td>
                        <input type="text" name="txtRoleName" id="txtRoleName" maxlength="200" style="width: 160px"
                            onchange="isExist()" /><span class="style1">*</span>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        备注
                    </td>
                    <td>
                        <textarea name="txtRemark" id="txtRemark" maxlength="200" style="width: 160px"></textarea>
                    </td>
                </tr>
            </table>
        </fieldset>
    </div>
    <div id="SetRole" title="权限设定">
        <fieldset>
            <div id="SetRoleHtml">
            </div>
        </fieldset>
    </div>
    <div id="SetUserGroup" title="用户组设定">
        <fieldset>
            <div id="SetUserGroupHtml">
                <!-- 用户组设定-->
                <table style="width: 100%; background-color: Silver" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <select multiple="multiple" id="listUserSource" style="width: 100px; height: 150px;">
                                <% foreach (var item in Get用户信息List())
                                   {%>
                                <option value='<%=item.ID %>'>
                                    <%=item.用户ID %>
                                </option>
                                <%}%>
                            </select>
                        </td>
                        <td style="background-color: Silver; text-align: center;">
                            <table style="width: 100%; background-color: Silver" cellpadding="1px" cellspacing="1px">
                                <tr>
                                    <td>
                                        <input type="button" value=">>" style="width: 30px" onclick="AddAllUserItem()" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="button" value=">" style="width: 30px" onclick="AddUserItem()" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="button" value="<" style="width: 30px" onclick="RemoveUserItem()" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="button" value="<<" style="width: 30px" onclick="RemoveAllUserItem()" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td align="right">
                            <select multiple="multiple" id="listUserTarget" style="width: 100px; height: 150px;">
                            </select>
                        </td>
                    </tr>
                </table>
                <!-- 用户组设定-->
            </div>
        </fieldset>
    </div>

    <script type="text/javascript">
        $("#RoleEdit").hide();
        $("#SetRole").hide();
        $("#SetUserGroup").hide();
    </script>

    </form>
</body>
</html>
